#!/usr/bin/perl -w

use IO::Socket;

my $arg0=shift;
my $extlist=shift;
my $debug=shift;

my $dest="$arg0";

my $to=10;

$SIG{ALRM}=\&timeout;

while(<STDIN>){
    chomp;
    ($p_host,$p_port)=split /:/;

    my $time1=time;

    $sock = new IO::Socket::INET (PeerAddr => $p_host,
                              PeerPort => $p_port,
			      Proto    => 'tcp',
			      Timeout  => $to
			  );


    if($sock){

	my $time2=time;
	
	my $mesg="GET http://$dest HTTP/1.1
	Host: $dest
	Accept: */*
	Pragma: no-cache
	User-Agent: aptitude.bbs\@bupt.org\n\n\n";

	$sock->print("$mesg");

	alarm($to);
	my $buffer=$sock->getline();
	alarm(0);
	if(! $buffer){next;}

	my $time3=time;
	
	chop($buffer);
	
	if($buffer =~ /HTTP\/1\.1 200 OK/){
	    my $ct=$time2-$time1;
	    my $rt=$time3-$time2;
	    my $td=$ct+$rt;
	    
	    print "$td\t$p_host:$p_port\n";
	}
	close($sock);
    }
}

open(EXT,"<$extlist");

while(<EXT>){
    chomp;
    ($p_host,$p_port)=split /:/;

    my $time1=time;

    $sock = new IO::Socket::INET (PeerAddr => $p_host,
                              PeerPort => $p_port,
			      Proto    => 'tcp',
			      Timeout  => $to
			  );


    if($sock){

	my $time2=time;
	
	my $mesg="GET http://$dest HTTP/1.1
	Host: $dest
	Accept: */*
	Pragma: no-cache
	User-Agent: aptitude.bbs\@bupt.org\n\n\n";

	$sock->print("$mesg");

	alarm($to);
	my $buffer=$sock->getline();
	alarm(0);
	if(! $buffer){next;}

	my $time3=time;
	
	chop($buffer);
	
	if($buffer =~ /HTTP\/1\.1 200 OK/){
	    my $ct=$time2-$time1;
	    my $rt=$time3-$time2;
	    my $td=$ct+$rt;
	    
	    print "$td\t$p_host:$p_port\n";
	}
	close($sock);
    }}

close(EXT);

exit;

sub timeout{
#    print STDERR "timeout\n";
    $sock->shutdown(2);
}
